package com.ymttest.business.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fenqile.api.pop.parameter.FenqileProductListGetBean;
import com.fenqile.api.pop.parameter.Product;
import com.fenqile.api.pop.service.FenqilePopCall;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.GetProductStatusBean;
import com.ymatou.iapi.thirdpartysync.parameter.PromotionDto;
import com.ymatou.iapi.thirdpartysync.parameter.QueryPromotionProductsBean;
import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymatou.iapi.thirdpartysync.service.GetProductStatusCall;
import com.ymatou.iapi.thirdpartysync.service.QueryPromotionProductsCall;
import com.ymatou.iapi.thirdpartysync.service.addProductCall;
import com.ymatou.open.product.parameter.GetDouyinProductInfoBean;
import com.ymatou.open.product.service.GetDouyinProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.model.thirdpartysync.promoter;
import com.ymttest.database.model.thirdpartysync.promoterproductcommission;
import com.ymttest.database.model.thirdpartysync.promotionproduct;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.DefThirdpartysyncWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.mybatisgenerator.Main;

public class ThirdpartysyncCallService {

	/***
	 * 创建码头商品
	 * @return
	 * @throws Exception
	 */
	public static addProductBean initAddProductBean() throws Exception
	{	
		//1：创建普通洋码头商品
		//2:检查数据是否同步到mongo
		//3:若同步到mongo,则添加到分期乐(前端查询是从mongo库中查询，然后添加到分期乐商品表)
		 addProductBean bean=new addProductBean();
		 int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
		//创建商品
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		addpro.getProduct().getCatalogList().remove(0);
		//addpro.getProduct().setSecondCategoryId(768);
		addpro.getProduct().setThirdCategoryId(1003);
		addpro.getProduct().setBrandId(ProductManagerCallServiceV3.BrandId);
		
		String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
		
		List<String> ids=new ArrayList();
		ids.add(productId);
		bean.setProductIdList(ids);
		
		return bean;
	}
	/**
	 *添加商品到分期乐
	 * @param bean
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static addProductCall AddProductCall(addProductBean bean,String productId) throws Exception
	{
		 ProductsWapper mongoDB_productsWapper=new ProductsWapper();
		 
	     addProductCall call=new addProductCall();
	     call.setData(false,bean);
		//查询mongo,确保monogo有数据再添加到分期乐
		int times=2000;
		do{
			Map<String,Object> p= mongoDB_productsWapper.getByProductId(productId);
			if((p==null ||p.size()==0 ))
			{
				Thread.sleep(times);
				p= mongoDB_productsWapper.getByProductId(productId);
				times+=2000;
			}else break;
		}while(times<=10000);
		
		if(times>10000)
			Logger.comment("商品未同步到mongo,无法添加分期乐商品");
		else 
			call.callService();
		Thread.sleep(2000);
		return call;
    }
	/**
	 * 查询分期乐同步数据
	 * @return
	 * @throws Exception
	 */
	public static Product searchFenqileSync(String fqlProductId) throws Exception{
		//休眠6秒目的是为了job同步商品到分期乐
		Thread.sleep(6000);
		
		FenqileProductListGetBean fenqileproductlistgetBean=new FenqileProductListGetBean();
		fenqileproductlistgetBean.setProduct_id(fqlProductId);
		fenqileproductlistgetBean.setFields("product_id,category_id,product_name,brand_id,brand_name,create_time,spu_status,pic_list,service,start_time,end_time,third_product_id");
		fenqileproductlistgetBean.setPage(1);
		fenqileproductlistgetBean.setPage_size(100);
		Logger.debug(FQLOpenCallService.FenqilePopCall("fenqile.product.detail.get", fenqileproductlistgetBean));
		Product fenqilePopCallToObject = FQLOpenCallService.FenqilePopCallTodetail("fenqile.product.detail.get", fenqileproductlistgetBean);
		System.out.println(fenqilePopCallToObject);
		return fenqilePopCallToObject;
		
	}
	
	
	/**
	 * 添加推广商品
	 * @return
	 * @throws Exception
	 */
	public static int insertPromotionProduct(String productId,int sellerId,double commission) throws Exception{
		
		promotionproduct product = new promotionproduct();
		product.setProductId(productId);
		product.setSellerId(sellerId);
		product.setCommission(new BigDecimal(commission));
		product.setEndTime(YMTDateUtil.getBeforeOrNextDate(10));
		product.setDyCategoryIds("1,2,3");
		product.setDyCategoryNames("a,b,c");
		
		DefThirdpartysyncWapper defThirdpartysyncWapper = new DefThirdpartysyncWapper();
		int insertPromotionProduct = defThirdpartysyncWapper.insertPromotionProduct(product);
		return insertPromotionProduct;
	}
	
	/**
	 * 设置推广费率
	 * @return
	 * @throws Exception
	 */
	public static int insertPromoterProductCommission(String productId,String pid,double commission) throws Exception{
		
		promoterproductcommission pc = new promoterproductcommission();
		pc.setEndTime(YMTDateUtil.getBeforeOrNextDate(10));
		pc.setProductId(productId);
		pc.setPid(pid);
		pc.setCommission(new BigDecimal(commission));
		
		DefThirdpartysyncWapper defThirdpartysyncWapper = new DefThirdpartysyncWapper();
		int insertPromotionProduct = defThirdpartysyncWapper.insertPromoterProductCommission(pc);
		return insertPromotionProduct;
	}
	
	/**
	 * 获取所有达人信息
	 * @return 
	 * @throws InterruptedException 
	 */
	public static List<promoter> GetAllpromoters() throws InterruptedException{
		return new DefThirdpartysyncWapper().selectAllpromoters();
	}
	
	/**
	 * 通过productid和name查询推广id
	 * @return
	 * @throws Exception
	 */
	public static int GetPromotionId(String productId,String productName) throws Exception{
		QueryPromotionProductsBean querypromotionproductsBean = new QueryPromotionProductsBean();
		QueryPromotionProductsCall querypromotionproductsCall = new QueryPromotionProductsCall();
		querypromotionproductsBean.setProductId(productId);
		querypromotionproductsBean.setProductName(productName);
		querypromotionproductsCall.setData(false,querypromotionproductsBean);
		querypromotionproductsCall.callService();			
		return querypromotionproductsCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList").get(0).getAsJsonObject().get("id").getAsInt();
	}
	
	
	/**
	 * 查询推广商品状态
	 * @return
	 * @throws Exception
	 */
	public static int GetProductStutus(String productId) throws Exception{
		GetProductStatusBean getproductstatusBean = new GetProductStatusBean();
		GetProductStatusCall getproductstatusCall = new GetProductStatusCall();
		ThirdpartysyncCallService.GetDouyinProductInfo(productId);
		getproductstatusBean.setProductIds(Arrays.asList(productId));
		getproductstatusCall.setData(false,getproductstatusBean);
		getproductstatusCall.callService();	
		return getproductstatusCall.getJsonObject().getAsJsonObject("data").getAsJsonObject("productList").get(productId).getAsInt();
	}
	
	/**
	 * 获取推广商品详情
	 * @return 
	 * @return
	 * @throws Exception
	 */
	public static List<PromotionDto> GetPromotionProducts(String productId,String productName) throws Exception{
		QueryPromotionProductsBean querypromotionproductsBean = new QueryPromotionProductsBean();
		QueryPromotionProductsCall querypromotionproductsCall = new QueryPromotionProductsCall();
		querypromotionproductsBean.setProductId(productId);
		querypromotionproductsBean.setProductName(productName);
		querypromotionproductsCall.setData(querypromotionproductsBean);
		querypromotionproductsCall.callService();			
		JsonArray products = querypromotionproductsCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
		return querypromotionproductsCall.getPromotionDtos();
	}
	
	/**
	 * 添加商品到抖音推广
	 * @return
	 * @throws Exception
	 */
	public static void GetDouyinProductInfo(String productId) throws Exception{
		
		GetDouyinProductInfoBean getDouyinProductInfoBean = new GetDouyinProductInfoBean();
		GetDouyinProductInfoCall getDouyinProductInfoCall = new GetDouyinProductInfoCall();
		
		getDouyinProductInfoBean.setApp_key("appid1331abcd");
		getDouyinProductInfoBean.setSign("aa");
		getDouyinProductInfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
		getDouyinProductInfoBean.setPid("pidabcd");
		
		getDouyinProductInfoCall.setData(false,getDouyinProductInfoBean);
		getDouyinProductInfoCall.callService();			
	}
	
	public static void main(String[] args) throws Exception {

		fenqileproductExample productExample=new fenqileproductExample();
		thirdpartysyncWapper syncWapper=new thirdpartysyncWapper(); 
		fenqilesku sku=new fenqilesku();
		fenqileskuExample skuExample=new fenqileskuExample();
		
		addProductBean bean=ThirdpartysyncCallService.initAddProductBean();
		String productId=bean.getProductIdList().get(0);
		
		ThirdpartysyncCallService.AddProductCall(bean,productId);
		
		productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
		
		skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
		
		int times=0;
		do{
		 	 if(prodcuts_DB.size()==0 )
			 {
		 	    times+=2000;
				Thread.sleep(times);
				prodcuts_DB=syncWapper.selectByExample(productExample);
				skus_DB=syncWapper.selectByExample(skuExample);
				Logger.comment("查询数据库休眠的时间："+times);
				if(prodcuts_DB.size()==0) continue;	
			 }else	 break;
	    }while(times<=10000);

		if(times>10000)
			Logger.comment("商品同步到分期乐异常");
		else {
			 ThirdpartysyncCallService.searchFenqileSync(prodcuts_DB.get(0).getFqlProductId());
		}
	}
}

